/*
 * Copyright (c) 2020-2020, org.smartboot. All rights reserved.
 * project name: smart-license
 * file name: LicenseTest.java
 * Date: 2020-03-26
 * Author: sandao (zhengjunweimail@163.com)
 */

package org.smartboot.license.generator;
import org.json.JSONObject;
import org.smartboot.license.server.LicenseServer;

import java.io.File;
import java.util.Base64;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;

public class LicenseGenerator {
//    public static void main(String[] args) throws Exception {
//        org.smartboot.license.server.LicenseServer server = new org.smartboot.license.server.LicenseServer(new File("source.txt"), new File("license.txt"));
//        String content = "{'productId':'rtest', 'applyUser':'dailey', 'applyCompany':'大连科德', 'functionItems': 'staticRotation'," +
//                " 'uuid':'13c10fb32f29f6f4cfef41e891ca990abc84bfbb26fc77cf4cfa0662bd2e16a9'}";
//        byte[] privateKey = Base64.getDecoder().decode("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKsjJq60wjdqdSJkL5uhWunFqwRASsXeFODtfOAX599lkad9NrEG+HlC/7/4fZNPdqN+bHA/9ytLeoMOqqAoOdk6PgBmxk/qJXZqpLRJ64iDWg+U/NecWS9zAaThXVRSuaDB1WExxXyDt7zZviM7dc+PSx3P/zbhgmzNGSBdVIuFAgMBAAECgYB+J6hhS0CMRz7XvlDWAekZwSwjMx2rpN1xPcA7FYRJSv9HH2eMsh3CjiAnrI8tY3uKZCydxpl24eoK6GnhLCYPhaPvYxKq7O1gB6fMDqe6PrOJWW0T09Jc3qS/aVZzEzCIFP/aMP0OrSaI5XV+kJM1yURj5h/1+rcbYZVIbK1sAQJBAOCFp2JjDrUuFNX283Q++a2w4gQjn8HNNzpU+0B9VErJQBHIX5aq2RvgzkxPnByijuTzl1/oGa6V6K3OO8fveoUCQQDDIXVjUE+QZMjY98Vuim7TjZHnD5xvPw0L4lCP93TkS3CHZbB36tkwetRlie8iVZcW7ZYvCl6RsNU8VTXDtB0BAkBEjUpgJfKVqy49bhtDQP8m4zz/GolquOfF7bCRLjMQaoYfZ9iXvi5v7qHOdvgpXhDXIvDzHYDJ54eVmcRy9VGpAkBI9ufoPQ2jVqxrxSqETStn29Yk6od6ab89JGEvlD6XZ0YquvGjxaN1J1jopw1Ob2bR4gF4kY4POGpj5xD1nxIBAkBPvWu3LwXnCa93+tl/llolADuCVJ2mE+cGDupwKLb0foLLjHgzYuetnVCwEu0zoOh72DSHUF+nQPfd9ygihvA0");
//        server.createLicense(content.getBytes(), new Date(1730451021305l), privateKey);
//    }
    private static final Pattern DATE_PATTERN = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
    public static boolean isValidDate(String date) {
        return DATE_PATTERN.matcher(date).matches();
    }

    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        System.out.print("************************************************\n");
        System.out.print("************License 生成工具*********************\n");
        System.out.print("************************************************\n");
        System.out.print("\n");
        System.out.print("请输入产品标识：   ");
        String productId = scanner.nextLine().trim();
        System.out.print("请输入申请人：  ");
        String applyUser = scanner.nextLine().trim();
        System.out.print("为哪个公司申请:  ");
        String applyCompany = scanner.nextLine().trim();
        System.out.print("请输入功能项：  ");
        String functionItems = scanner.nextLine().trim();
        if(functionItems.equalsIgnoreCase("")){
            functionItems = "staticRotationalAccuracy";
        }
        System.out.print("请输入运行电脑唯一标识：  ");
        String uuid = scanner.nextLine().trim();
        System.out.print("请输入授权天数（自当前时刻开始）:  ");
        long days = scanner.nextInt();
        scanner.close();

        JSONObject json = new JSONObject();
        json.put("productId", productId);
        json.put("applyUser", applyUser);
        json.put("applyCompany", applyCompany);
        json.put("functionItems", functionItems);
        json.put("uuid", uuid);

        LicenseServer server = new LicenseServer(new File("source.txt"), new File("license.txt"));
        String content = json.toString();
        byte[] privateKey = Base64.getDecoder().decode("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKsjJq60wj" +
                "dqdSJkL5uhWunFqwRASsXeFODtfOAX599lkad9NrEG+HlC/7/4fZNPdqN+bHA/9ytLeoMOqqAoOdk6PgBmxk/qJXZqpLRJ64iDWg+" +
                "U/NecWS9zAaThXVRSuaDB1WExxXyDt7zZviM7dc+PSx3P/zbhgmzNGSBdVIuFAgMBAAECgYB+J6hhS0CMRz7XvlDWAekZwSwjMx2r" +
                "pN1xPcA7FYRJSv9HH2eMsh3CjiAnrI8tY3uKZCydxpl24eoK6GnhLCYPhaPvYxKq7O1gB6fMDqe6PrOJWW0T09Jc3qS/aVZzEzCIFP" +
                "/aMP0OrSaI5XV+kJM1yURj5h/1+rcbYZVIbK1sAQJBAOCFp2JjDrUuFNX283Q++a2w4gQjn8HNNzpU+0B9VErJQBHIX5aq2Rvgzkx" +
                "PnByijuTzl1/oGa6V6K3OO8fveoUCQQDDIXVjUE+QZMjY98Vuim7TjZHnD5xvPw0L4lCP93TkS3CHZbB36tkwetRlie8iVZcW7ZYv" +
                "Cl6RsNU8VTXDtB0BAkBEjUpgJfKVqy49bhtDQP8m4zz/GolquOfF7bCRLjMQaoYfZ9iXvi5v7qHOdvgpXhDXIvDzHYDJ54eVmcRy9" +
                "VGpAkBI9ufoPQ2jVqxrxSqETStn29Yk6od6ab89JGEvlD6XZ0YquvGjxaN1J1jopw1Ob2bR4gF4kY4POGpj5xD1nxIBAkBPvWu3Lw" +
                "XnCa93+tl/llolADuCVJ2mE+cGDupwKLb0foLLjHgzYuetnVCwEu0zoOh72DSHUF+nQPfd9ygihvA0");
        server.createLicense(content.getBytes(), new Date(System.currentTimeMillis() + 3600 * 24 * days * 1000), privateKey);
    }

}
